package cn.it.shop.action;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.it.shop.model.FileImage;
import cn.it.shop.service.AccountService;
import cn.it.shop.service.CategoryService;
import cn.it.shop.service.ProductService;
import cn.it.shop.util.FileUpload;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class BaseAction<T> extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware, ModelDriven<T> {
	protected T						model;
	protected Integer				page;
	protected Integer				rows;
	protected String				ids;
	protected FileImage				fileImage;
	@Resource
	protected FileUpload			fileUpload;
	protected List<T>				jsonList	= null;
	protected InputStream			inputStream;
	protected Map<String, Object>	pageMap		= null;

	@Resource
	protected AccountService		accountService;
	@Resource
	protected CategoryService		categoryService;
	@Resource
	protected ProductService		productService;

	protected Map<String, Object>	application;
	protected Map<String, Object>	request;
	protected Map<String, Object>	session;

	
	
	public FileUpload getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(FileUpload fileUpload) {
		this.fileUpload = fileUpload;
	}

	public FileImage getFileImage() {
		return fileImage;
	}

	public void setFileImage(FileImage fileImage) {
		this.fileImage = fileImage;
	}

	public List<T> getJsonList() {
		return jsonList;
	}

	public Map<String, Object> getPageMap() {
		return pageMap;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public T getModel() {
		ParameterizedType type = (ParameterizedType) this.getClass()
				.getGenericSuperclass();
		Class clazz = (Class) type.getActualTypeArguments()[0];

		System.out.println("================测试案例=====================");
		System.out.println("this.getClass().getGenericSuperclass()返回的类型:"
				+ type);
		System.out.println("type.getActualTypeArguments()[0]返回的类型:" + clazz);
		try {
			model = (T) clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return model;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
